package Utils;

import Bean.MindMap;

import java.io.*;

public class FileUtils {
    public static void storeMindMap(MindMap mindMap) throws IOException {
        File file = new File(PropertiesUtils.getStoreFilePath());
        if(!file.exists()){
            file.mkdirs();
        }
        File f = null;
        FileOutputStream fosOfMap = null;
        ObjectOutputStream objOpOfMap = null;
            f = new File(PropertiesUtils.getStoreFilePath()+"//"+mindMap.getCenter().getMtext()+".mindmap");
            fosOfMap = new FileOutputStream(f);
            objOpOfMap = new ObjectOutputStream(fosOfMap);
            objOpOfMap.writeObject(mindMap);
        objOpOfMap.close();
    }

    public static MindMap getMindMap(File targetFile) {
        FileInputStream fis = null;
        ObjectInputStream objIP = null;
        MindMap target = null;
        try{
            fis = new FileInputStream(targetFile);
            objIP = new ObjectInputStream(fis);
            target = (MindMap) objIP.readObject();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(objIP!=null) {
                    objIP.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return target;
    }
}

